#!/usr/bin/env node

const inquirer = require('inquirer')
const path = require("path");
const { exec } = require('child_process')
const fs = require('fs')
const templateList = require(`${__dirname}/../template`)
const { showTable } = require(`${__dirname}/../util/showTable`)
const symbols = require('log-symbols')
const chalk = require('chalk')
chalk.level = 1

let question = [
    {
        name: "name",
        message: "请输入要删除的模板名称",
        validate(val) {
            if (!val) {
                return 'Name is required!'
            } else if (!templateList[val]) {
                return 'Template does not exist!'
            } else {
                return true
            }
        }
    }
]

inquirer
    .prompt(question).then(answers => {
        let { name } = answers;
        // 删除template.json 文件的内容
        delete templateList[name]
        fs.writeFile(`${__dirname}/../template.json`, JSON.stringify(templateList), 'utf-8', err => {
            if (err) console.log(chalk.red(symbols.error), chalk.red(err))
            console.log('\n')
            console.log(chalk.green(symbols.success), chalk.green('Deleted successfully!\n'))
            console.log(chalk.green('The latest templateList is: \n'))
            showTable(templateList)
        })
        //删除本地的文件
        exec(`rmdir /s/q  ` + path.resolve(__dirname, `../templates/${name}`), (error, stdout, stderr) => {
            if (error) { // 当有错误时打印出错误并退出操作
                console.log(chalk.red(error))
                process.exit()
            }
            console.log(chalk.green('初始化完成'))
            process.exit() // 退出这次命令行操作
        })
    })